using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace CatchTheCat
{
    /// <summary>
    ///     This is the Player Component
    /// </summary>
    public class Spieler : Microsoft.Xna.Framework.DrawableGameComponent
    {
        // Member Variablen ===================================================

        enum eRichtung { HOCH=0, RUNTER, RECHTS, LINKS };
        private eRichtung BlickRichtung;

        private const int TextureHeight = 20;
        private const int TextureWidth = 20;

        public Vector2 Position;
        private Texture2D SpielerTextur;
        private float Geschwindigkeit;

        private KeyboardState TastaturEingabe;

        // Constructor ========================================================

        public Spieler(Game game) : base(game)
        {
            // Inits ==========================================================
            Position.X = 100;
            Position.Y = 100;

            Geschwindigkeit = 300;

            BlickRichtung = eRichtung.RUNTER;
        }

        public override void Initialize()
        {
            // TODO: Add your initialization code here
            base.Initialize();

        }

        public void LoadContent( ContentManager Content)
        {
            SpielerTextur = Content.Load<Texture2D>("spieler2");
        }

        protected override void UnloadContent()
        {
            
        }

        public override void Update(GameTime gameTime)
        {
            TastaturEingabe = Keyboard.GetState();

            // check Buttons                                                              
            if (TastaturEingabe.IsKeyDown(Keys.Right))
            {
                Position.X += Geschwindigkeit * (float)gameTime.ElapsedGameTime.TotalSeconds; //the amount of time, since the last frame
                BlickRichtung = eRichtung.RECHTS;
            }
            if (TastaturEingabe.IsKeyDown(Keys.Left))
            {
                Position.X -= Geschwindigkeit * (float)gameTime.ElapsedGameTime.TotalSeconds;
                BlickRichtung = eRichtung.LINKS;
            }
            if (TastaturEingabe.IsKeyDown(Keys.Up))
            {
                Position.Y -= Geschwindigkeit * (float)gameTime.ElapsedGameTime.TotalSeconds;
                BlickRichtung = eRichtung.HOCH;
            }
            if (TastaturEingabe.IsKeyDown(Keys.Down))
            {
                Position.Y += Geschwindigkeit * (float)gameTime.ElapsedGameTime.TotalSeconds;
                BlickRichtung = eRichtung.RUNTER;
            }

            // check border-Collision 
            if (Position.X < 0)                                   Position.X = 0;
            if ( (Position.X + TextureWidth) > Game1.WindowWidth) Position.X = (Game1.WindowWidth - TextureWidth);

            if (Position.Y < 0)                                    Position.Y = 0;
            if ((Position.Y + TextureHeight) > Game1.WindowHeight) Position.Y = (Game1.WindowHeight - TextureHeight);
        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            
            spriteBatch.Begin();

            // for "spieler" - Textur
            //spriteBatch.Draw(SpielerTextur, Position, Color.White);

            // for "spieler2"
            spriteBatch.Draw(SpielerTextur, Position, new Rectangle( (int)((TextureWidth-1) * (int)BlickRichtung), 0, TextureWidth-1, TextureHeight-1), Color.White);

            spriteBatch.End();
        }
    }
}
